extends Sprite2D

var speed = 200
var velocity = Vector2(200, 200)
var screen_size

func _ready():
	screen_size = get_viewport_rect().size
	# 随机位置
	position = Vector2(
		randf_range(0, screen_size.x),
		randf_range(0, screen_size.y)
	)
	# 随机纹理颜色
	modulate = Color(randf(), randf(), randf())

func _process(delta):
# 每帧更新位置
	position += velocity * delta

	# 获取 `CollisionShape2D` 的形状类型（CircleShape2D）
	var circle_shape = $Area2D/CollisionShape2D.shape as CircleShape2D
	var radius = circle_shape.radius  # 圆的半径
	
	# 检查是否碰到屏幕边缘
	if position.x - radius <= 0 or position.x + radius >= screen_size.x:
		velocity.x = -velocity.x  # 水平方向反弹
		# 强制将其位置推回到屏幕内
		if position.x - radius <= 0:
			position.x = radius  # 防止卡在左边缘
		elif position.x + radius >= screen_size.x:
			position.x = screen_size.x - radius  # 防止卡在右边缘

	if position.y - radius <= 0 or position.y + radius >= screen_size.y:
		velocity.y = -velocity.y  # 垂直方向反弹
	# 强制将其位置推回到屏幕内
		if position.y - radius <= 0:
			position.y = radius  # 防止卡在上边缘
		elif position.y + radius >= screen_size.y:
			position.y = screen_size.y - radius  # 防止卡在下边缘
